# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""label_image for tflite."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import argparse
import numpy as np
import time
from PIL import Image
from concurrent.futures import ThreadPoolExecutor

from cv2 import VideoCapture
import cv2
# import tensorflow as tf # TF2
import tflite_runtime.interpreter as tflite
import threading
import time
import os
import pygame
from playsound import playsound
data=[0,0,0,0]
lock = threading.Lock()
THRESH_HOLD=0.2

pool = ThreadPoolExecutor(max_workers=1)
pool_volume=ThreadPoolExecutor(max_workers=1)
def audio_play(src):
    print(src)
    pygame.mixer.init()
    pygame.mixer.music.load(src)
    pygame.mixer.music.play(0)
    time.sleep(8)
    pygame.mixer.music.unload()

    pygame.mixer.quit()
    time.sleep(8)

def load_labels(filename):
    with open(filename, 'r') as f:
        return [line.strip() for line in f.readlines()]
def reset(args):
        time.sleep(22)
        if args==0:


            os.system("./gpio 498 0")
        elif args==1:
            os.system("./gpio 499 0")
        elif args==2:
            os.system("./gpio 432 0")
        elif args==3:
            os.system("./gpio 434 0")

        
def upload():
    global data
    while True:
        if max(data)>40:
            index=data.index(max(data))
            if index==0:
                print("可回收物打开")
                pygame.mixer.init()

                # sound1=pygame.mixer.Sound("assets/TES.mp3")
                pool_volume.submit(audio_play,"assets/recyclable.wav")
                
                # sound1.play()

                pool.submit(reset, 0)

                os.system("./gpio 498 1")
                # pool_volume.submit(audio_play,"assets/recyclable.wav")
                
            elif index==1:
                print("其他垃圾打开")
                os.system("./gpio 499 1")
                pool.submit(reset, 1)

                pool_volume.submit(audio_play,"assets/residual.wav")

                # playsound("assets/residual.wav",True)

            elif index==2:
                print("有害垃圾打开")
                os.system("./gpio 432 1")

                pool.submit(reset, 2)

                pool_volume.submit(audio_play,"assets/hazardous.wav")


                # playsound("assets/hazardous.wav",True)

            elif index==3:
                print("厨余垃圾打开")
                os.system("./gpio 434 1")
                pool.submit(reset,2)

                pool_volume.submit(audio_play,"assets/food.wav")
                # playsound("assets/food.wav",True)
            lock.acquire()

            data=[0,0,0,0]
            lock.release()

            time.sleep(10)


           

if __name__ == '__main__':
    os.system("./gpio 498 0")
    os.system("./gpio 499 0")
    os.system("./gpio 432 0")
    os.system("./gpio 434 0")
    interpreter = tflite.Interpreter(model_path="./final/model.tflite")
    interpreter.allocate_tensors()
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
    # check the type of the input tensor
    floating_model = input_details[0]['dtype'] == np.float32

    # os.system(" aplay -Dplughw:2,0 assets/ondevice.wav")
    # playsound("assets/ondevice.wav",True)
    # sound = pygame.mixer.Sound("assets/ondevice.wav")
    # sound.play()
    # time.sleep(6)
    pygame.mixer.init()
    pygame.mixer.music.load("assets/ondevice.wav")
    pygame.mixer.music.play(0)
    time.sleep(5)
    pygame.mixer.music.unload()

    pygame.mixer.quit()

    # NxHxWxC, H:1, W:2
    t1=threading.Thread(target=upload,)
    t1.start()  
    while True:
        time.sleep(0.5)
        data[0]+=1
        print('-------------------------------------')



        
